﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace SuperKit
{
    public enum ClipType {
        Point,
        Span,
    }
    public class ClipBase {
        
        public virtual string Time { get; }
        public virtual ClipType ClipType { get; }

        private bool _checked;
        public bool Checked {
            get => _checked;
            set => _checked = value;
        }

        public virtual Visibility CheckVisibility { get; }

        public ClipBase() {
            _checked = true;
        }
    }
    public class ClipPoint : ClipBase {
        public int Point { get; }
        public override string Time => Kit.FormatTime(Point);
        public override ClipType ClipType => ClipType.Point;
        public override Visibility CheckVisibility => Visibility.Collapsed;

        public ClipPoint(int point) {
            Point = point;
            
        }

    }

    public class ClipSpan : ClipBase {
        public int Start { get; set; }
        public int End { get; set; }
        public override string Time => string.Format("{0}-{1}", Kit.FormatTime(Start), Kit.FormatTime(End));
        public override ClipType ClipType => ClipType.Span;
        public override Visibility CheckVisibility => Visibility.Visible;

        public ClipSpan(int start, int end) {
            Start = start;
            End = end;
        }

        public void SetValue(int start, int end) {
            Start = start;
            End = end;            
        }
    }

    public class ClipCatalog {
        public string Name { get; set; }
        public ObservableCollection<ClipBase> Clips { get; set; }

        public ClipCatalog(string name) {
            Name = name;
            Clips = new ObservableCollection<ClipBase>();
        }
    }

    public class ClipCollections {
        public List<ClipSpan> spans;
        public List<ClipPoint> points;
        public int Duration { get; set; }


        public ClipCollections() {
            spans = new List<ClipSpan>();
            points = new List<ClipPoint>();
        }

        public void AddCut(int current, int length) {
            Duration = length;
            points.Add(new ClipPoint(current));
            Calculate();
        }

        private void Calculate() {
            // 检查没有项目的特殊情况
            if (points.Count == 0) {
                spans.Clear();
                return;
            }

            points.Sort((a, b) => {
                return a.Point.CompareTo(b.Point);
            });

            // 从0到最后，依次建立项目
            int start = 0;
            int index = 0;
            foreach (var i in points) {
                Add(start, i.Point, index++);
                start = i.Point;
            }
            // 追加到结束位置
            Add(points[^1].Point, Duration, index++);

            // 删除多余位置
            while (spans.Count > index) {
                spans.RemoveAt(index);
            }
        }

        public void Fill(ClipCatalog points_list, ClipCatalog spans_list) {
            points_list.Clips.Clear();
            foreach (var i in points) {
                points_list.Clips.Add(i);
            }

            spans_list.Clips.Clear();
            foreach (var i in spans) {
                spans_list.Clips.Add(i);
            }
        }

        private void Add(int start, int end, int index) {
            // 检查是否拥有位置，如果没有，增加一个
            if (spans.Count < index + 1) {
                spans.Add(new ClipSpan(0, 0));
            }
            // 得到索引位置处并设置
            spans[index].SetValue(start, end);
        }

        public void RemovePoint(ClipPoint item) {
            points.Remove(item);
            Calculate();
        }
    }
}
